package com.exmaple.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.exmaple.springcloud.apis.PayFeignApi;
import com.exmaple.springcloud.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer")
public class OrderSentinelController {
    @Autowired
    PayFeignApi payFeignApi;

    //  blockHandler: 声明熔断时执行的降级方法（限流熔断降级）
    // fallback: 抛出异常执行的降级方法（异常降级）
    @GetMapping(value = "/pay/sentinel/fc/{id}")
    @SentinelResource(value = "sentinel_fc" ,blockHandler = "orderBlockHandler" ,fallback = "orderExceptionHandler")
    public R<String> sentinelFC(@PathVariable("id") Integer id){
        return payFeignApi.sentinelFC(id);
    }

    //熔断规则测试
    @GetMapping(value = "/pay/sentinel/circuit/{id}")
    @SentinelResource(value = "sentinel_circuit" ,blockHandler = "orderBlockHandler" ,fallback = "orderExceptionHandler")
    public R<String> sentinelCircuit(@PathVariable("id")Integer id){
        return payFeignApi.sentinelCircuit(id);
    }

    /**
     * 定义降级逻辑
     * 熔断执行的降级方法
     */
    public R<String> orderBlockHandler(@PathVariable("id") Integer id, BlockException b){

        return R.fail("1888","触发熔断的降级方法: 系统繁忙，请稍后重试\n  ##########熔断执行############");
    }
    /**
     * 定义降级逻辑
     * 抛出异常执行的降级方法
     */
    public R<String> orderExceptionHandler(@PathVariable("id") Integer id,Throwable e){
        return R.fail("500","抛出异常执行的降级方法: 系统繁忙，请稍后重试 \n ============抛出异常执行===========");


    }



}
